package br.edu.ufcg.arrowfollow.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import br.edu.ufcg.arrowfollow.R;

public class GameView extends SurfaceView {
	private Bitmap mBmp;
	private SurfaceHolder mHolder;
	private GameLoopThread gameLoopThread;
	private int x = 0;
	private int y = 0;
	private boolean invertX = false;
	private boolean invertY = false;

	public GameView(Context context) {
		super(context);
		gameLoopThread = new GameLoopThread(this);
		mHolder = getHolder();
		mHolder.addCallback(new Callback() {

			public void surfaceDestroyed(SurfaceHolder arg0) {
				// TODO Auto-generated method stub

			}

			public void surfaceCreated(SurfaceHolder arg0) {
				gameLoopThread.setRunning(true);
				gameLoopThread.start();
			}

			public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
					int arg3) {
				// TODO Auto-generated method stub

			}
		});
		mBmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawColor(Color.BLACK);
		if (x <= getWidth() - mBmp.getWidth() && !invertX) {
			x++;
		} else if (x >= 0 && invertX) {
			x--;
		} else {
			invertX = !invertX;
		}

		if (y <= getHeight() - mBmp.getHeight() && !invertY) {
			y++;
		} else if (y >= 0 && invertY) {
			y--;
		} else {
			invertY = !invertY;
		}
		canvas.drawBitmap(mBmp, x, y, null);
	}

	public void stop() {
		this.gameLoopThread.destroy();
	}

}
